﻿Imports System.Text

''' <summary>
''' Socket 收发数据状态
''' </summary>
''' <remarks></remarks>
Public Class SocketInfo

    ''' <summary>
    ''' 数据发送方向
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum eDataType
        ''' <summary>
        ''' 此类的数据是从客户端接收到的数据
        ''' </summary>
        ''' <remarks></remarks>
        dRead

        ''' <summary>
        ''' 此类的数据时需要发送到客户端的数据
        ''' </summary>
        ''' <remarks></remarks>
        dWrite
    End Enum
    ''' <summary>
    ''' 客户端 Socket
    ''' </summary>
    ''' <remarks></remarks>
    Public [Socket] As Net.Sockets.Socket = Nothing

    ''' <summary>
    ''' 接收客户端发送数据的缓存最大值
    ''' </summary>
    ''' <remarks></remarks>
    Public Const BufferSize As Integer = 2000

    ''' <summary>
    ''' 数据收发缓存
    ''' </summary>
    ''' <remarks></remarks>
    Public buffer() As Byte

    ''' <summary>
    ''' 数据收发缓存 明文
    ''' </summary>
    ''' <remarks></remarks>
    Public Stringbuffer As New StringBuilder


    ''' <summary>
    ''' 往客户端发送的数据明文
    ''' </summary>
    ''' <remarks></remarks>
    Public DataType As eDataType = eDataType.dWrite



    ''' <summary>
    ''' 客户端是否还在线
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsConnected() As Boolean
        If [Socket] Is Nothing Then Return False
        Return True
        'If [Socket].Poll(50, SelectMode.SelectError) Then
        '    Return False
        'Else
        '    Return True
        'End If
    End Function


    ''' <summary>
    ''' 输出远程连接端信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function toString() As String
        If [Socket] Is Nothing Then
            Return "NULL"
        Else
            Return [Socket].RemoteEndPoint.ToString
        End If
    End Function


    Public mKey As String

    ''' <summary>
    ''' 输出本地连接端信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function toLocalString() As String
        If [Socket] Is Nothing Then
            Return "NULL"
        Else
            Try
                Return [Socket].LocalEndPoint.ToString

            Catch ex As Exception

                Return "Err"
            End Try
        End If
    End Function
End Class